src/factorial.js
zzmq.dev@gmail.com[dev3]/mathkit[add-factorial-n-function-tests] / src / factorial.js
@ 03f29f63
Download
Ladder Summary
- file Add factorial utility function with comprehensive tests
/**
* Computes the factorial of a non‑negative integer.
*
* @param {number} n - The number to compute the factorial of.
* @returns {number|undefined} The factorial of n for valid inputs, otherwise undefined.
*/
export function factorial(n) {
if (typeof n !== "number" || !Number.isFinite(n)) return undefined;
if (!Number.isInteger(n)) return undefined;
if (n < 0) return undefined;
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}